﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HRMS_Model;
using DataManager;
using System.Diagnostics;
using System.Web.Mvc;
using HRMS.Models.ModelBinders;

namespace HRMS.Models
{
    public class ExaminationsModels
    {
        public static bool AddExamination(Examination examination)
        {
            try
            {
                using (var exmEntity = new HRMSEntities())
                {
                    exmEntity.Examinations.AddObject(examination);
                    exmEntity.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }

        public static List<Examination> GetExaminations(string sortExpression, string sortDirection, int pageIndex, int pageSize, int pageCount)
        {
            using (var exmEntity = new HRMSEntities())
            {
                return exmEntity.Examinations.OrderBy("it." + sortExpression + " " + sortDirection).Skip(pageIndex * pageSize).Take(pageCount * pageSize).ToList();
            }
        }
         
       public static int GetExaminationsCount()
        {
            using (var exmEntity = new HRMSEntities())
            {
                return exmEntity.Examinations.Count();
            }
        }

        public static bool UpdateExamination([ModelBinder(typeof(CustomModelBinder))]Examination examination)
        {
            try
            {
                using (var exmEntity = new HRMSEntities())
                {
                    var exmToEdit = exmEntity.Examinations.FirstOrDefault(r => r.QuestionId == examination.QuestionId);
                    if (exmToEdit != null)
                    {
                        exmToEdit.Question = examination.Question;
                        exmToEdit.OptionA = examination.OptionA;
                        exmToEdit.OptionB = examination.OptionB;
                        exmToEdit.OptionC = examination.OptionC;
                        exmToEdit.Answer = examination.Answer;
                        exmToEdit.Isactive = examination.Isactive;
                        exmToEdit.SkillId = examination.SkillId;
                        //empToEdit.Department.DepartmentName
                    }
                    exmEntity.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }

        public static bool DeleteExamination(int id)
        {
            try
            {
                using (var exmEntity = new HRMSEntities())
                {
                    exmEntity.Examinations.DeleteObject(exmEntity.Examinations.First(e => e.QuestionId == id));
                    exmEntity.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }



         
    }
}
        
    
